package com.bwsoft.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.bwsoft.admin.domain.NewsCategory;
import com.bwsoft.admin.domain.valueObject.NewsCategoryVO;
import com.bwsoft.admin.service.NewsCategoryService;
import com.bwsoft.domain.Dictionary;
import com.bwsoft.domain.DictionaryData;
import com.bwsoft.model.Message;

@Service(NewsCategoryService.NAME)
public class NewsCategoryServiceImpl extends BaseServiceImpl_ybd implements NewsCategoryService {

	private static Logger LOG = Logger.getLogger(NewsCategoryServiceImpl.class.getClass());

	@Override
	public List<NewsCategory> findAll() {
		String categorysHql = "from NewsCategory order by order asc";
		List<NewsCategory> newsCategorys=newsCategoryDao.findByHql(categorysHql);
		return newsCategorys;
	}
	
	@Override
	public List<NewsCategory> findData(Integer page, Integer rows) {
		String newsCategorysHql = "from NewsCategory order by order asc";
		List<NewsCategory> newsCategorys=newsCategoryDao.findPage(newsCategorysHql, rows, (page - 1) * rows);
		//List<NewsCategory> newsCategorys=newsCategoryDao.findAll();
		return newsCategorys;
	}

	@Override
	public Long getTotalCount() {
		Long count;
		String newsCategorysHql = "select count(*) from NewsCategory ";
		count = (Long)newsCategoryDao.findByHql(newsCategorysHql).get(0);
		return count;
	}

	@Override
	public Message saveOrUpdateNewsCategory(NewsCategory newsCategory) {
		Message msg = new Message();
		try {
			newsCategoryDao.saveOrUpdate(newsCategory);
			msg.setMsgFlag(true);
			msg.setMsgStr("保存成功！");
		} catch (Exception e) {
			msg.setMsgFlag(false);
			msg.setMsgStr("保存失败：" + e.toString());
			LOG.info(e);
		}
		return msg;
	}

	@Override
	public Message removeNewsCategory(Integer id) {
		Message msg = new Message();
//		//List<NewsCategory> newsCategorys=new ArrayList<NewsCategory>();
		NewsCategory newsCategory=newsCategoryDao.findById(id);
		Long count;
		String newsHql = "select count(*) from News news where news.category.id = '"+id+"'";
		count = (Long)newsDao.findByHql(newsHql).get(0);
		if(count > 0){
			msg.setMsgFlag(false);
			msg.setMsgStr("删除失败：请先删除该新闻类别项下的所有值！");
		}
		else{
			try{
				newsCategoryDao.delete(newsCategory);
				msg.setMsgFlag(true);
				msg.setMsgStr("删除成功！");
			}catch (Exception e) {
				msg.setMsgFlag(false);
				msg.setMsgStr("删除失败："+e);
			}
		}
		return msg;
	}

	@Override
	public NewsCategory newsCategoryModifyInfo(Integer id) {
		NewsCategory newsCategory=null;
		try{
			newsCategory=newsCategoryDao.findById(id);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return newsCategory;
	}

	@Override
	public Message checkNewsCategoryInput(NewsCategory newsCategory) {
		Message msg = new Message();
		try {
			
			//拼装查询HQL
			StringBuffer sb=new StringBuffer();
			sb.append("from NewsCategory nwc where 1=1 ");
			if(newsCategory.getName()!=null&&!newsCategory.getName().trim().equals("")){
				sb.append(" and nwc.name='"+newsCategory.getName()+"'");
			}
			if(newsCategory.getId()!=null){
				sb.append(" and nwc.id !='"+newsCategory.getId()+"'");
			}
			LOG.info("检查字典的Form表单重复的HQL"+sb.toString());
			//根据拼装的hql来查询Dictionary集合
			List<Dictionary> dicList=newsCategoryDao.findByHql(sb.toString());
			if(dicList.size()==0){
				msg.setMsgFlag(true);
				msg.setMsgStr("数据库中暂无此值");
			}else{
				msg.setMsgFlag(false);
				msg.setMsgStr("数据库已存在此值");
			}
		} catch (Exception e) {
			msg.setMsgFlag(false);
			msg.setMsgStr("添加失败：" + e.toString());
			LOG.info(e);
		}
		return msg;
	}

	@Override
	public List<NewsCategoryVO> getVo(List<NewsCategory> ncs) {

		List<NewsCategoryVO> newsCategoryVoList = new ArrayList<NewsCategoryVO>();
		NewsCategoryVO newsCategoryVo = new NewsCategoryVO();
		for (NewsCategory nc : ncs) {
			newsCategoryVo = new NewsCategoryVO();
			newsCategoryVo.setId(nc.getId());
			newsCategoryVo.setName(nc.getName());
			newsCategoryVo.setOrder(nc.getOrder());
			newsCategoryVo.setStatus(nc.isStatus());
			newsCategoryVoList.add(newsCategoryVo);
		}
		
		return newsCategoryVoList;
	}

	
}